<?php

namespace app\controller;

use support\Db;
use support\Request;
use Tinywan\Storage\Storage;
use Webman\Captcha\CaptchaBuilder;
use yzh52521\hash\Hash;


class CommonController extends BaseController
{

    /**
     * 输出验证码图像
     */
    public function captcha(Request $request)
    {
        // 初始化验证码类
        $builder = new CaptchaBuilder();
        // 生成验证码
        $builder->build();
        // 将验证码的值存储到session中
        $request->session()->set('captcha', strtolower($builder->getPhrase()));
        // 获得验证码图片二进制数据
        $img_content = $builder->get();

        // 输出验证码二进制数据
        return response($img_content, 200, ['Content-Type' => 'image/jpeg']);
    }

    public function getInfo(){
        return '{
    "code": 200,
    "msg": "操作成功",
    "permissions": [
        "*:*:*"
    ],
    "roles": [
        "admin"
    ],
    "user": {
        "createTime": "2021-12-29T08:12:02.053Z",
        "updateTime": "2023-02-23T08:25:29.000Z",
        "createBy": "",
        "updateBy": "",
        "remark": "",
        "userId": 1,
        "userName": "admin",
        "nickName": "admin",
        "userType": "00",
        "email": "87789771@qq.com",
        "phonenumber": "13006133172",
        "sex": "0",
        "avatar": "/upload/2023-02-23/1677140729991-772556076-blob.png",
        "status": "0",
        "delFlag": "0",
        "loginIp": "",
        "loginDate": null,
        "dept": {
            "createTime": "2021-12-29T08:36:06.320Z",
            "updateTime": "2022-01-06T05:39:00.000Z",
            "createBy": "",
            "updateBy": "",
            "remark": "",
            "deptId": 1,
            "deptName": "淳渔总部",
            "orderNum": 1,
            "leader": "淳渔",
            "phone": "15674874636",
            "email": "542968439@qq.com",
            "status": "0",
            "delFlag": "0"
        },
        "posts": [
            {
                "createTime": "2022-01-06T05:39:28.615Z",
                "updateTime": "2022-01-06T05:39:28.615Z",
                "createBy": "",
                "updateBy": "",
                "remark": "",
                "postId": 1,
                "postCode": "ceo",
                "postName": "董事长",
                "postSort": 1,
                "status": "0"
            }
        ],
        "roles": [
            {
                "createTime": "2021-12-29T08:34:08.369Z",
                "updateTime": "2022-01-06T05:38:18.155Z",
                "createBy": "",
                "updateBy": "",
                "remark": "",
                "roleId": 1,
                "roleName": "超级管理员",
                "roleKey": "admin",
                "roleSort": 1,
                "dataScope": "1",
                "menuCheckStrictly": false,
                "deptCheckStrictly": true,
                "status": "0",
                "delFlag": "0"
            }
        ]
    }
}';
    }

    public function getRouters(){
        return '{
    "code": 200,
    "msg": "操作成功",
    "data": [
        {
            "hidden": false,
            "component": "Layout",
            "path": "/",
            "meta": {
                "noCache": true,
                "link": null
            },
            "children": [
                {
                    "name": "Column",
                    "hidden": false,
                    "component": "column/index",
                    "path": "column",
                    "meta": {
                        "title": "栏目管理",
                        "icon": "tree-table",
                        "noCache": false,
                        "link": null
                    }
                }
            ]
        },
        {
            "hidden": false,
            "component": "Layout",
            "path": "/",
            "meta": {
                "noCache": true,
                "link": null
            },
            "children": [
                {
                    "name": "Web-user",
                    "hidden": false,
                    "component": "webUser/index",
                    "path": "web-user",
                    "meta": {
                        "title": "网站用户",
                        "icon": "user",
                        "noCache": false,
                        "link": null
                    }
                }
            ]
        },
        {
            "name": "Movie",
            "hidden": false,
            "redirect": "noRedirect",
            "component": "Layout",
            "alwaysShow": true,
            "path": "/movie",
            "meta": {
                "title": "影视管理",
                "icon": "dashboard",
                "noCache": false,
                "link": null
            },
            "children": [
                {
                    "name": "Movie-list",
                    "hidden": false,
                    "component": "movie/list/index",
                    "path": "movie-list",
                    "meta": {
                        "title": "影视列表",
                        "icon": "list",
                        "noCache": false,
                        "link": null
                    }
                }
            ]
        },
        {
            "name": "Actor",
            "hidden": false,
            "redirect": "noRedirect",
            "component": "Layout",
            "alwaysShow": true,
            "path": "/actor",
            "meta": {
                "title": "影人管理",
                "icon": "people",
                "noCache": false,
                "link": null
            },
            "children": [
                {
                    "name": "Actor-list",
                    "hidden": false,
                    "component": "actor/list/index",
                    "path": "actor-list",
                    "meta": {
                        "title": "影人列表",
                        "icon": "list",
                        "noCache": false,
                        "link": null
                    }
                }
            ]
        },
        {
            "name": "Video",
            "hidden": false,
            "redirect": "noRedirect",
            "component": "Layout",
            "alwaysShow": true,
            "path": "/video",
            "meta": {
                "title": "视频管理",
                "icon": "redis-list",
                "noCache": false,
                "link": null
            },
            "children": [
                {
                    "name": "List",
                    "hidden": false,
                    "component": "video/list/index",
                    "path": "list",
                    "meta": {
                        "title": "视频列表",
                        "icon": "list",
                        "noCache": false,
                        "link": null
                    }
                }
            ]
        },
        {
            "name": "Basic",
            "hidden": false,
            "redirect": "noRedirect",
            "component": "Layout",
            "alwaysShow": true,
            "path": "/basic",
            "meta": {
                "title": "基础数据",
                "icon": "tab",
                "noCache": false,
                "link": null
            },
            "children": [
                {
                    "name": "Country",
                    "hidden": false,
                    "component": "basic/country/index",
                    "path": "country",
                    "meta": {
                        "title": "国家管理",
                        "icon": "peoples",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Language",
                    "hidden": false,
                    "component": "basic/language/index",
                    "path": "language",
                    "meta": {
                        "title": "语言管理",
                        "icon": "nested",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Genre",
                    "hidden": false,
                    "component": "basic/genre/index",
                    "path": "genre",
                    "meta": {
                        "title": "类型管理",
                        "icon": "row",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Profession",
                    "hidden": false,
                    "component": "basic/profession/index",
                    "path": "profession",
                    "meta": {
                        "title": "影人职务",
                        "icon": "peoples",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Level",
                    "hidden": false,
                    "component": "basic/level/index",
                    "path": "level",
                    "meta": {
                        "title": "家长引导",
                        "icon": "server",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Banner",
                    "hidden": false,
                    "component": "basic/banner/index",
                    "path": "banner",
                    "meta": {
                        "title": "横幅管理",
                        "icon": "druid",
                        "noCache": false,
                        "link": null
                    }
                }
            ]
        },
        {
            "name": "System",
            "hidden": false,
            "redirect": "noRedirect",
            "component": "Layout",
            "alwaysShow": true,
            "path": "/system",
            "meta": {
                "title": "系统管理",
                "icon": "system",
                "noCache": false,
                "link": null
            },
            "children": [
                {
                    "name": "User",
                    "hidden": false,
                    "component": "system/user/index",
                    "path": "user",
                    "meta": {
                        "title": "用户管理",
                        "icon": "user",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Role",
                    "hidden": false,
                    "component": "system/role/index",
                    "path": "role",
                    "meta": {
                        "title": "角色管理",
                        "icon": "peoples",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Menu",
                    "hidden": false,
                    "component": "system/menu/index",
                    "path": "menu",
                    "meta": {
                        "title": "菜单管理",
                        "icon": "tree-table",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Dept",
                    "hidden": false,
                    "component": "system/dept/index",
                    "path": "dept",
                    "meta": {
                        "title": "部门管理",
                        "icon": "tree",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Post",
                    "hidden": false,
                    "component": "system/post/index",
                    "path": "post",
                    "meta": {
                        "title": "岗位管理",
                        "icon": "post",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Dict",
                    "hidden": false,
                    "component": "system/dict/index",
                    "path": "dict",
                    "meta": {
                        "title": "字典管理",
                        "icon": "dict",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Config",
                    "hidden": false,
                    "component": "system/config/index",
                    "path": "config",
                    "meta": {
                        "title": "参数设置",
                        "icon": "edit",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Notice",
                    "hidden": false,
                    "component": "system/notice/index",
                    "path": "notice",
                    "meta": {
                        "title": "通知公告",
                        "icon": "message",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Log",
                    "hidden": false,
                    "redirect": "noRedirect",
                    "component": "ParentView",
                    "alwaysShow": true,
                    "path": "log",
                    "meta": {
                        "title": "日志管理",
                        "icon": "log",
                        "noCache": false,
                        "link": null
                    },
                    "children": [
                        {
                            "name": "Operlog",
                            "hidden": false,
                            "component": "monitor/operlog/index",
                            "path": "operlog",
                            "meta": {
                                "title": "操作日志",
                                "icon": "form",
                                "noCache": false,
                                "link": null
                            }
                        },
                        {
                            "name": "Logininfor",
                            "hidden": false,
                            "component": "monitor/logininfor/index",
                            "path": "logininfor",
                            "meta": {
                                "title": "登录日志",
                                "icon": "logininfor",
                                "noCache": false,
                                "link": null
                            }
                        }
                    ]
                }
            ]
        },
        {
            "name": "Monitor",
            "hidden": false,
            "redirect": "noRedirect",
            "component": "Layout",
            "alwaysShow": true,
            "path": "/monitor",
            "meta": {
                "title": "系统监控",
                "icon": "monitor",
                "noCache": false,
                "link": null
            },
            "children": [
                {
                    "name": "Online",
                    "hidden": false,
                    "component": "monitor/online/index",
                    "path": "online",
                    "meta": {
                        "title": "在线用户",
                        "icon": "online",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Cache",
                    "hidden": false,
                    "component": "monitor/cache/index",
                    "path": "cache",
                    "meta": {
                        "title": "缓存管理",
                        "icon": "redis",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Job",
                    "hidden": false,
                    "component": "monitor/job/index",
                    "path": "job",
                    "meta": {
                        "title": "定时任务",
                        "icon": "job",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Server",
                    "hidden": false,
                    "component": "monitor/server/index",
                    "path": "server",
                    "meta": {
                        "title": "服务监控",
                        "icon": "server",
                        "noCache": false,
                        "link": null
                    }
                }
            ]
        },
        {
            "name": "Tool",
            "hidden": false,
            "redirect": "noRedirect",
            "component": "Layout",
            "alwaysShow": true,
            "path": "/tool",
            "meta": {
                "title": "系统工具",
                "icon": "tool",
                "noCache": false,
                "link": null
            },
            "children": [
                {
                    "name": "Build",
                    "hidden": true,
                    "component": "tool/build/index",
                    "path": "build",
                    "meta": {
                        "title": "表单构建",
                        "icon": "build",
                        "noCache": false,
                        "link": null
                    }
                },
                {
                    "name": "Swagger",
                    "hidden": false,
                    "component": "tool/swagger/index",
                    "path": "swagger",
                    "meta": {
                        "title": "系统接口",
                        "icon": "swagger",
                        "noCache": false,
                        "link": null
                    }
                }
            ]
        }
    ]
}';
    }

    public function getHash(Request $request)
    {
        var_dump((Hash::check('123456', '$2y$10$63Ah672h77uarQKsedj7geDkxQfhvfGV3Nk.POtZ1tIAbBqP346Zq')) );
        // 获取post请求中的captcha字段
        //$pass = $request->post('captcha');
       // echo;
        return json(['code' => 0, 'msg' =>  Hash::make('123456')]);
    }

    /**
     * 检查验证码
     */
    public function check(Request $request)
    {
        // 获取post请求中的captcha字段
        $captcha = $request->post('captcha');
        // 对比session中的captcha值
        if (strtolower($captcha) !== $request->session()->get('captcha')) {
            return json(['code' => 400, 'msg' => '输入的验证码不正确']);
        }
        return json(['code' => 0, 'msg' => 'ok']);
    }

    public function index(Request $request)
    {
        echo 44;
       // Db::ta
        $users = Db::table('movie_basic')->get();
        return   json(['code' => 0, 'msg' => $users]);
    }


    public function upload(Request $request)
    {
        $file = $request->file()['file'];
        var_dump($request->file());

        //$data = Storage::uploadFile()[0];
        var_dump($file->getUploadMimeType());
        $dir = public_path().'/files/myfile';
        if(!is_dir($dir)){
            mkdir($dir,777,true);
        }
        $ext = empty($file->getUploadExtension())?$this->getExteionByMInetype($file->getUploadMimeType()):$file->getUploadExtension();
        $url = '/files/myfile.'.$ext;
        $file->move($url);
        var_dump($url);
        move_uploaded_file($file->getPath(), $url);
//        $url = str_replace('//'.$request->host(),'',$data['url']);
//        $url = str_replace('http:','',$url);
//        $url = str_replace('https:'.$request->host(),'',$url);
        $data['fileName'] = "/assets".$url;
        $data['code'] =200;//
        return   json($data);
    }

  function  getExteionByMInetype($mimeType){
    // Map MIME types to file extensions
    $mimeTypesToExtensions = [
    'image/png' => 'png',
    'image/jpeg' => 'jpg',
    'image/gif' => 'gif',
    //'image/gif' => 'gif',
        // Add other MIME types and their corresponding extensions here
    ];

    // Get the file extension based on the MIME type
    return $extension = isset($mimeTypesToExtensions[$mimeType]) ? $mimeTypesToExtensions[$mimeType] : '';
    }

    /**
     * 国家列表
     * @param Request $request
     * @return \support\Response
     */
    public function countryList(Request $request)
    {
        $users = Db::table('basic_country')->get();
        return   json($this->success('',$users));
    }

    public function view(Request $request)
    {
        return view('index/view', ['name' => 'webman']);
    }

    public function json(Request $request)
    {
        return json(['code' => 0, 'msg' => 'ok']);
    }

}
